	Ext.BLANK_IMAGE_URL = "../script/ext-2.0.2/resources/images/default/s.gif"; 
   
	var roleStore = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({url: 'userInfo!getRole.action?dateTime='+new Date().getTime()}),
        reader: new Ext.data.JsonReader({
            root: 'param',
            fields:["key","value"]
        }),
        remoteSort: true
    }); 
    
	roleStore.load();
	
	var roleSelect = new Ext.form.ComboBox({
						  id:'roleCodeValue',
						  name:'roleCodeValue',
						  fieldLabel: '角色',
					      hiddenName: 'roleCode',
					      store: roleStore,
					      valueField:'key',
				          displayField:'value',
				          typeAhead: true,
				          mode: 'local',
				          triggerAction: 'all',
				          resizable:true,
				          emptyText:'请选择角色...',
				          allowBlank:false,
				          anchor:'95%',
				          forceSelection:true
						});
						
	var orgTreeLoader = new Ext.tree.TreeLoader({dataUrl:'../util/orgTree!getAllTree.action?dateTime='+new Date().getTime()});
	
	var orgTree = new Ext.ux.ComboBoxTree({
			name:'orgId1',
			hiddenName:'orgId1',
			fieldLabel: '机构',
			resizable:true,
			width :200,
			height:300,
			tree : {
				xtype:'treepanel',
				loader: orgTreeLoader,
	       	 	root : new Ext.tree.AsyncTreeNode({id:'0',draggable:false,text:'机构树'})
	    	},
	    	emptyText:'请选择机构...',
			selectNodeModel:'exceptRoot'
		});	
	
	var orgTreeTemp = new Ext.ux.ComboBoxTree({
			name:'userOrg',
			hiddenName:'userOrg',
			fieldLabel: '机构',
			resizable:true,
			anchor:'95%',
			tree : {
				xtype:'treepanel',
				loader: orgTreeLoader,
	       	 	root : new Ext.tree.AsyncTreeNode({id:'0',draggable:false,text:'机构树'})
	    	},
	    	emptyText:'请选择机构...',
			selectNodeModel:'exceptRoot',
			allowBlank:false
		});
		
	var userNameTemp = new Ext.form.TextField({ 
		id:'userNameTemp',
		name:'userNameTemp',
		width:100
		});
		
	var storeUserManage = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
        	url: 'userInfo!getList.action?dateTime='+new Date().getTime()
        }),
        reader: new Ext.data.JsonReader({
            root: 'ZANY',
            totalProperty: "results",
            fields:["userName","userCode","roleName","userOrg","sts","lastDate","createDate","creater"]
        }),
        sortInfo:{field:'harmDate', direction:'DESC'},// 排序信息
        remoteSort: true
    });
    
    storeUserManage.load();
    
	var sm = new Ext.grid.CheckboxSelectionModel();
	
	var colUserManage=new Ext.grid.ColumnModel([sm,
		{header:"用户名",dataIndex:"userName",width:10,sortable:true},
		{header:"用户ID",dataIndex:"userCode",width:10,sortable:true},
		{header:"角色",dataIndex:"roleName",width:10,sortable:true},
		{header:"机构",dataIndex:"userOrg",width:10,sortable:true},
		{header:"状态",dataIndex:"sts",width:10,sortable:true},
		{header:"上次登录时间",dataIndex:"lastDate",width:10,sortable:true},
		{header:"创建时间",dataIndex:"createDate",width:10,sortable:true},
		{header:"创建人",dataIndex:"creater",width:10,sortable:true}
		]);
	
	var oneTbar = new Ext.Toolbar({   
        items: ['-',{text:'&nbsp;&nbsp;查&nbsp;&nbsp;询&nbsp;&nbsp;',iconCls:'yes',handler:getList},
			    '-',{text:'&nbsp;&nbsp;重&nbsp;&nbsp;置&nbsp;&nbsp;',iconCls:'refresh',handler:resetQuery},
			    '-',{text:'新增用户',iconCls:'add',handler:insertinfo},
				'-',{text:'禁止使用',iconCls:'remove',handler:updateNO},
				'-',{text:'恢复使用',iconCls:'add',handler:updateYES},
				'-',{text:'重置密码',iconCls:'add',handler:update}
        	   ]   
    });
    				
	var userManage = new Ext.grid.GridPanel({
		id:'userManage',
		title :'[用户管理]信息维护',
		height:500,
		stripeRows :true,
		bodyStyle:'width:100%',
        disableSelection:true,
		cm: colUserManage,
		viewConfig: {
            forceFit:true
        },
		store: storeUserManage,
		iconCls: 'details',
		animCollapse: true,
		trackMouseOver:false, 
        loadMask: {msg:'正在加载[用户管理]信息，请稍侯……'},
		selModel:sm, 
		tbar:['-',new Ext.form.Label({text:'姓名:'}),'-',userNameTemp,
			  '-',new Ext.form.Label({text:'所属机构：'}),'-',orgTree			  
		],
		listeners:{  
			'render':function(){   
				oneTbar.render(this.tbar); //add one tbar        
			}
		},
		bbar: new Ext.PagingToolbar({
	            pageSize: 20,
	            store: storeUserManage,
	            displayInfo: true,
	        	emptyMsg:'没有记录'
	        })
	});
	
	function update(){
		if (Ext.getCmp("userManage").getSelectionModel().hasSelection()) {
			Ext.MessageBox.confirm('Message','请问您真的要重置密码？',function(btn){
        	if(btn == 'yes'){	
	            var ids = new Array();
	            var records = Ext.getCmp("userManage").getSelectionModel().getSelections();
	            for (var i = 0, len = records.length; i < len; i++) {
	                ids[i] = records[i].data["userCode"];
	            }
	            Ext.Ajax.request({url:'userInfo!update.action?dateTime='+new Date().getTime(),
	                params:{updateId:ids},
	                success:function(response, options){
	                   var response = Ext.util.JSON.decode(response.responseText);
						if(response.success){
	                    	ZANY.util.Msg('信息',"重置密码成功！");
						}else if(!response.success){
							ZANY.util.Msg('信息',"重置密码失败，如有问题请联系系统管理员！");  
						}             
	                }
	        	});
			}
		})}else{
			ZANY.util.Msg('提示信息',"请选择用户后再操作！");   
		}
	}
    
	var fp = new Ext.FormPanel({
						id:'ext_form',
				        labelWidth: 100, 
				        waitMsg:'正在提交，请稍后...',
				        frame:true,
				        bodyStyle:'padding:5px 5px 0',
				        width: 400,
				        reader: new Ext.data.JsonReader({
				            root: 'ZANY',
				            totalProperty: "results",
				            fields:["userName","userCode","roleCode","userOrg"]
				        }),
				        items: [{
				        		 layout:'column',
							     layout: 'form',
							     defaultType: 'textfield',
							     items: [{
											fieldLabel: '用户名',
											name: 'userName',
											id:'userName',
											maxLength:10,
											anchor:'95%',
											allowBlank:false
						                },
						                {
											fieldLabel: '用户ID',
											name: 'userCode',
											id:'userCode',
											maxLength:10,
											anchor:'95%',
											allowBlank:false
						                },
						                orgTreeTemp,
						                roleSelect,
						                new Ext.form.Hidden({name:'flag'}),
						                new Ext.form.Hidden({name:'userCodeTemp'})
								        ]
				        		}] 
	    			});
	    			
	var winKey =new Ext.Window({
						id:"ext_win",
        				title: '[新增用户]',
						width: 400,
						closeAction:'hide',
						modal:true,
						iconCls: 'add',
						buttons: [{
				            text: '提交',
				            handler: function(){
				            	if(fp.form.isValid()){
				            		fp.form.submit({
					                    url: 'userInfo!insert.action?dateTime='+new Date().getTime(),
										waitTitle:"新增用户",
					                    waitMsg: '正在提交数据,请稍后... ...',
					                    success: function(){
											Ext.getCmp('userManage').getStore().reload(); //刷新Grid
											fp.form.reset();
							                storeUserManage.modified = [];//将修改过的记录置为空，如果不清空，则修改过的数据会一直保留
											Ext.MessageBox.alert('提示信息', '维护用户成功！'); 
											winKey.hide();
										},
										failure:function(form,action){
											Ext.MessageBox.alert('提示信息', '维护用户失败,请联系系统管理员!'+action.result.msg);
										}
					                });
				            	}else{
				            		ZANY.util.Msg('添加提示','数据不完整！');
				            	}
				            }
				        },{
				            text: '取消',handler: function(){winKey.hide();fp.form.reset();}
				        }],
						items:[fp]	
					});
					
	function insertinfo(){
		winKey.show();	
		orgTreeTemp.setValue('');
		roleSelect.setValue('');
		fp.getForm().findField("userName").getEl().dom.readOnly = false;
		fp.getForm().findField("userCode").getEl().dom.readOnly = false;
    }
    
    userManage.addListener('rowdblclick',function(e){
    	if (Ext.getCmp("userManage").getSelectionModel().hasSelection()) {
    			getDetail();
			}
		},userManage
	);
	
	function getDetail(){
		var records = Ext.getCmp("userManage").getSelectionModel().getSelected();
		var userCode =records.data["userCode"];
		fp.getForm().load({url:'userInfo!getList.action?dateTime='+new Date().getTime()+'&userCode='+userCode+'&flag=update', waitMsg:'Loading'});
    	winKey.show();
    	fp.getForm().findField("userCode").getEl().dom.readOnly = true;
    	fp.getForm().findField("userName").getEl().dom.readOnly = true;
    	fp.getForm().findField("flag").setValue("update");
    	fp.getForm().findField("userCodeTemp").setValue(userCode);
	}
	
	function updateNO(){
		if (Ext.getCmp("userManage").getSelectionModel().hasSelection()) {
			Ext.MessageBox.confirm('Message','请问您真的要禁止使用么？',function(btn){
        	if(btn == 'yes'){	
	            var ids = new Array();
	            var records = Ext.getCmp("userManage").getSelectionModel().getSelections();
	            for (var i = 0, len = records.length; i < len; i++) {
	                ids[i] = records[i].data["userCode"];
	            }
	            Ext.Ajax.request({url:'userInfo!updateSts.action?dateTime='+new Date().getTime()+'&sts=0',
	                params:{updateId:ids},
	                success:function(response, options){
	                   var response = Ext.util.JSON.decode(response.responseText);
						if(response.success){
					        Ext.getCmp('userManage').getStore().reload(); //刷新Grid
	                    	ZANY.util.Msg('信息',"禁止使用成功！");
						}else if(!response.success){
							ZANY.util.Msg('信息',"禁止使用失败，如有问题请联系系统管理员！");  
						}             
	                }
	        	});
			}
		})}else{
			ZANY.util.Msg('提示信息',"请选择用户后再操作！");   
		}
	}
	
	function updateYES(){
		if (Ext.getCmp("userManage").getSelectionModel().hasSelection()) {
			Ext.MessageBox.confirm('Message','请问您真的要恢复使用么？',function(btn){
        	if(btn == 'yes'){	
	            var ids = new Array();
	            var records = Ext.getCmp("userManage").getSelectionModel().getSelections();
	            for (var i = 0, len = records.length; i < len; i++) {
	                ids[i] = records[i].data["userCode"];
	            }
	            Ext.Ajax.request({url:'userInfo!updateSts.action?dateTime='+new Date().getTime()+'&sts=1',
	                params:{updateId:ids},
	                success:function(response, options){
	                   var response = Ext.util.JSON.decode(response.responseText);
						if(response.success){
					        Ext.getCmp('userManage').getStore().reload(); //刷新Grid
	                    	ZANY.util.Msg('信息',"恢复使用成功！");
						}else if(!response.success){
							ZANY.util.Msg('信息',"恢复使用失败，如有问题请联系系统管理员！");  
						}             
	                }
	        	});
			}
		})}else{
			ZANY.util.Msg('提示信息',"请选择用户后再操作！");   
		}
	}
	
	function resetQuery(){
		Ext.getCmp("userNameTemp").setValue('');
		orgTree.setValue('');
	}
	
	function getList(){
		var params={};
	    params.userName = Ext.get('userNameTemp').getValue();
	    params.orgId = orgTree.getValue();
	    storeUserManage.baseParams=params;
	    storeUserManage.load({
	    	callback : function(r, options, success) {   
		        if (success == false) {   
		            Ext.Msg.alert("错误", storeUserManage.reader.jsonData.msg);
		        }   
		    }
		});
	}
	
	Ext.onReady(function(){
		Ext.QuickTips.init(); 
	　　 Ext.form.Field.prototype.msgTarget = 'side';
			var port = new Ext.Viewport({
				layout : 'fit',
				frame : true,
				items : [this.userManage]
	    	});
	});